|
|
|
|
|
|
|
|
A QueryDef is a part of the database that stores the query definition. This definition is the SQL statement that you create.  |
|
|
|
|
|
|
|
|
|
|
The SELECT statement retrieves records (or specified fields from records) and places the information in a dynaset or table for further processing by a program. The SELECT statement follows this general form: |
|
|
|
|
|
|
|
|
SELECT [predicate] fieldlist FROM tablelist [table relations]
[range options] [sort options] [group options] |
|
|
|
|
|
|
|
|
|
In my demonstrations of code statements, words in all caps are SQL keywords, and italicized words or phrases are used to indicate terms that a programmer would replace in an actual statementfor example, fieldlist would be replaced with Lastname, Firstname. Phrases or words inside square brackets are optional terms.  |
|
|
|
|
|
|
|
|
|
|
The various components of the preceding statement are explained in this chapter. Although a SQL statement can be greatly complex, it also can be fairly simple. The simplest form of the SELECT statement is shown here: |
|
|
|
|
|
|
|
|
Defining the Desired Fields |
|
|
|
|
|
|
|
|
The fieldlist part of the SELECT statement is used to define the fields to be included in the output recordset. You can include all fields in a table, selected fields from the table, or even calculated fields based on other fields in the table. You can also choose the fields to be included from a single table or from multiple tables. |
|
|
|
|
|
|
|
|
The fieldlist portion of the SELECT statement takes the following form: |
|
|
|
|
|
|
|
|
[tablename.]field1 [AS alt1][,[tablename.]field2 [AS alt2]] |
|
|
|
|
|
|
|
|
Selecting All Fields from a Table |
|
|
|
|
|
|
|
|
The *, or wild-card parameter, is used to indicate that you want to select all the fields in the specified table. The wild card is used in the fieldlist portion of the statement. The statement SELECT * FROM Sales, when used with the sample database you are developing, produces the output recordset shown in Figure C.1. |
|
|
|
|
|
|
|
|
Selecting Individual Fields from a Table |
|
|
|
|
|
|
|
|
Frequently, you need only a few fields from a table. You can specify the desired fields by including a field list in the SELECT statement. Within the field list, the individual fields are separated by commas. In addition, if the desired field has a space in the name, as in Order Quantity, the field name must be enclosed within square brackets, [].The recordset that results from the following SELECT statement is shown in Figure C.2. A recordset created with fields specified is more efficient than one created with the wild card (*), both in terms of the size of the recordset and speed of creation. As a general rule, you should limit your queries to the smallest number of fields that can accomplish your purpose. |
|
|
|
|
|
|
|
|
SELECT [Item Code], Quantity FROM Sales |
|
|
|
|
|